package com.bolt.auth.security.admin.biz.sys.controller;

import com.bolt.auth.admin.api.sys.dto.DictDetailDTO;
import com.bolt.auth.admin.api.sys.dto.converter.DictDetailConverter;
import com.bolt.auth.admin.api.sys.entity.DictDetailEntity;
import com.bolt.auth.security.admin.biz.sys.service.DictDetailService;
import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.support.spring.jpa.specification.QueryHelp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@Api(tags = "系统：字典详情管理")
@RequestMapping("/api/dict/detail")
public class DictDetailController {

    @Autowired
    private DictDetailService dictDetailService;

    @ApiOperation("查询字典详情")
    @GetMapping("/{dictName}")
    public ResultMessage query(@PathVariable String dictName) {
        return Results.success(dictDetailService.queryByDictName(dictName));
    }

    /**
     * 数据字典详情列表分页
     *
     * @return
     */
    @GetMapping(value = "/page")
    @ApiOperation(notes = "数据字典详情列表分页数据字典详情列表分页", httpMethod = "GET", value = "数据字典详情列表分页")
    public ResultMessage list(Pageable pageable, DictDetailDTO dto) {
        PageInfo<DictDetailEntity> pageInfo = dictDetailService.findPage(pageable, (root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root, dto, criteriaBuilder));
        return Results.success(pageInfo.map(K -> DictDetailConverter.toDTO(K)));

    }

    /**
     * 根据ID数据字典详情操作
     */
    @GetMapping("/get/{id}")
    @ApiOperation(notes = "根据ID查询数据字典详情", httpMethod = "GET", value = "根据ID查询数据字典详情")
    public ResultMessage <DictDetailDTO> get(@PathVariable Long id) {
        DictDetailEntity detailEntity = dictDetailService.findOne(id).get();
        return Results.success(DictDetailConverter.toDTO(detailEntity));
    }

    /**
     * 数据字典详情编辑操作
     *
     * @param dao 表单数据
     * @return
     */
    @PostMapping
    @ApiOperation(notes = "数据字典详情编辑", httpMethod = "POST", value = "数据字典详情编辑")
    public ResultMessage <Void> saveOrUpdate(@Validated @RequestBody DictDetailDTO dao) {
        dictDetailService.saveOrUpdate(dao);
        return Results.success();
    }

    /**
     * 数据字典详情删除操作
     */
    @DeleteMapping
    @ApiOperation(notes = "根据ID删除数据字典详情", httpMethod = "POST", value = "根据ID删除数据字典详情")
    public ResultMessage <Void> doDelete(@RequestBody Set<Long> ids) {
        for (Long id : ids) {
            dictDetailService.deleteById(id);
        }
        return Results.success();
    }

}

 